package cn.bld.controller;

import cn.bld.entity.StorageRecord;
import cn.bld.entity.SysSupplier;
import cn.bld.entity.SysUser;
import cn.bld.service.SysStorageRecordService;
import cn.bld.service.SysSupplierService;
import cn.bld.utils.MyFileUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/storage")
public class SysStorageController {
    @Autowired
    private SysStorageRecordService sysStorageRecordService;
    @Autowired
    private SysSupplierService sysSupplierService;

    @GetMapping("page")
    public String page(@RequestParam(value = "queryGoodsName", required = false, defaultValue = "") String queryGoodsName,
                       @RequestParam(value = "querySupplierId", required = false, defaultValue = "0") Integer querySupplierId,
                       @RequestParam(value = "queryPayStatus", required = false, defaultValue = "0") Integer queryPayStatus,
                       @RequestParam(value = "pageIndex", required = false, defaultValue = "1") Integer pageIndex, Model model) {
        Integer size = 8;
        int totalCount = sysStorageRecordService.findCount(queryGoodsName, querySupplierId,queryPayStatus);
        int totalPageCount = totalCount % size == 0 ? totalCount / size : totalCount / size + 1;
        List<StorageRecord> storageRecordList = sysStorageRecordService.findByCondition(queryGoodsName, querySupplierId, queryPayStatus,pageIndex, size);
        model.addAttribute("totalCount", totalCount);
        model.addAttribute("totalPageCount", totalPageCount);
        model.addAttribute("currentPageNo", pageIndex);
        model.addAttribute("supplierList", sysSupplierService.findall());
        model.addAttribute("storageRecordList", storageRecordList);
        model.addAttribute("queryGoodsName", queryGoodsName);
        model.addAttribute("querySupplierId", querySupplierId);
        model.addAttribute("querypayStatus", queryPayStatus);

        return "storageRecord/list";
    }
    @GetMapping("/add")
    public String add(){
        return "storageRecord/add";
    }
    @PostMapping("toAdd")
    public String addSupplier(StorageRecord storageRecord,  HttpSession session) throws IOException {
        SysUser loginUser = (SysUser) session.getAttribute("userSession");
        storageRecord.setCreatedUserId(loginUser.getId());
        int i = sysStorageRecordService.insertStorage(storageRecord);
        if (i==1){
            return "redirect:/storage/page";
        }
        return "redirect:/WEB-INF/jsp/storageRecord/add.jsp";
    }
    @GetMapping("/view/{opr}/{id}")
    public String view(@PathVariable Integer id, Model model, @PathVariable String opr){
        model.addAttribute(("storageRecord"),sysStorageRecordService.findById(id));
        if("v".equals(opr)){
            return "/storageRecord/view";
        }else {
            return "/storageRecord/update";
        }

    }
    @PostMapping("update")
    public String update(StorageRecord storageRecord,HttpSession session){
        SysUser loginUser = (SysUser) session.getAttribute("userSession");
        storageRecord.setUpdatedUserId(loginUser.getId());
        int i = sysStorageRecordService.updateStorage(storageRecord);
        return "redirect:/storage/page";
    }


    @DeleteMapping("del/{storageRecordId}")
    @ResponseBody
    public String del(@PathVariable Integer storageRecordId){
        Map<String,Object> map = new HashMap<>();
        if (sysStorageRecordService.findById(storageRecordId)==null){
            map.put("delResult","notexist");
            return JSON.toJSONString(map);
        }
        int i = sysStorageRecordService.deleteStorage(storageRecordId);
        if (i==1){
            map.put("delResult","true");
        }else {
            map.put("delResult","false");
        }
        return JSON.toJSONString(map);
    }
}